package com.woniu.hospital.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.woniu.hospital.service.ISurgicalDescService;
import com.woniu.hospital.service.UserService;
import com.woniu.user.domain.Doctor;
import com.woniu.user.domain.Patient;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.hospital.domain.Surgical;
import com.woniu.hospital.service.ISurgicalService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 手术记录表Controller
 * 
 * @author wolf-six
 * @date 2023-02-18
 */
@RestController
@RequestMapping("/surgical")
public class SurgicalController extends BaseController
{
    @Autowired
    private ISurgicalService surgicalService;

    @Autowired
    private ISurgicalDescService iSurgicalDescService;

    @Autowired
    private UserService userService;

    /**
     * 查询手术记录表列表
     */
    @RequiresPermissions("hospital:surgical:list")
    @GetMapping("/list")
    public TableDataInfo list(Surgical surgical) throws InvocationTargetException, IllegalAccessException {
        startPage();
        List<Surgical> list = surgicalService.selectSurgicalList(surgical);
        for (Surgical item:list) {

             item.setDoctorNamel(userService.selDoctorById(item.getDocterId()).getDoctorName());

             item.setPatientName(userService.getPatientById(item.getPatientId()).getPatientName());
             item.setSurgicaName(iSurgicalDescService.selectSurgicalDescBySurgicalDescid(item.getSurgicalDescid()).getSurgicalDescname());

        }

        return getDataTable(list);
    }

    /**
     * 导出手术记录表列表
     */
    @RequiresPermissions("hospital:surgical:export")
    @Log(title = "手术记录表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Surgical surgical)
    {
        System.out.println("null");
        List<Surgical> list = surgicalService.selectSurgicalList(surgical);
        ExcelUtil<Surgical> util = new ExcelUtil<Surgical>(Surgical.class);
        util.exportExcel(response, list, "手术记录表数据");
    }

    /**
     * 获取手术记录表详细信息
     */
    @RequiresPermissions("hospital:surgical:query")
    @GetMapping(value = "/{surgicalId}")
    public AjaxResult getInfo(@PathVariable("surgicalId") Long surgicalId)
    {
        return success(surgicalService.selectSurgicalBySurgicalId(surgicalId));
    }

    /**
     * 新增手术记录表
     */
    @RequiresPermissions("hospital:surgical:add")
    @Log(title = "手术记录表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Surgical surgical)
    {
        return toAjax(surgicalService.insertSurgical(surgical));
    }

    /**
     * 修改手术记录表
     */
    @RequiresPermissions("hospital:surgical:edit")
    @Log(title = "手术记录表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Surgical surgical)
    {
        return toAjax(surgicalService.updateSurgical(surgical));
    }

    /**
     * 删除手术记录表
     */
    @RequiresPermissions("hospital:surgical:remove")
    @Log(title = "手术记录表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{surgicalIds}")
    public AjaxResult remove(@PathVariable Long[] surgicalIds)
    {
        return toAjax(surgicalService.deleteSurgicalBySurgicalIds(surgicalIds));
    }
}
